{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Partition Array According to Given Pivot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #two-pointers #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #双指针 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: pivotArray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #根据给定数字划分数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>pivot</code>&nbsp;。请你将&nbsp;<code>nums</code>&nbsp;重新排列，使得以下条件均成立：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>所有小于&nbsp;<code>pivot</code>&nbsp;的元素都出现在所有大于&nbsp;<code>pivot</code>&nbsp;的元素&nbsp;<strong>之前</strong>&nbsp;。</li>\n",
    "\t<li>所有等于&nbsp;<code>pivot</code>&nbsp;的元素都出现在小于和大于 <code>pivot</code>&nbsp;的元素 <strong>中间</strong>&nbsp;。</li>\n",
    "\t<li>小于 <code>pivot</code>&nbsp;的元素之间和大于 <code>pivot</code>&nbsp;的元素之间的 <strong>相对顺序</strong>&nbsp;不发生改变。\n",
    "\t<ul>\n",
    "\t\t<li>更正式的，考虑每一对&nbsp;<code>p<sub>i</sub></code>，<code>p<sub>j</sub></code>&nbsp;，<code>p<sub>i</sub></code>&nbsp;是初始时位置 <code>i</code>&nbsp;元素的新位置，<code>p<sub>j</sub></code>&nbsp;是初始时位置&nbsp;<code>j</code>&nbsp;元素的新位置。对于小于&nbsp;<code>pivot</code>&nbsp;的元素，如果&nbsp;<code>i &lt; j</code>&nbsp;且&nbsp;<code>nums[i] &lt; pivot</code> 和&nbsp;<code>nums[j] &lt; pivot</code>&nbsp;都成立，那么&nbsp;<code>p<sub>i</sub> &lt; p<sub>j</sub></code>&nbsp;也成立。类似的，对于大于&nbsp;<code>pivot</code>&nbsp;的元素，如果&nbsp;<code>i &lt; j</code> 且&nbsp;<code>nums[i] &gt; pivot</code> 和&nbsp;<code>nums[j] &gt; pivot</code>&nbsp;都成立，那么&nbsp;<code>p<sub>i</sub> &lt; p<sub>j</sub></code>&nbsp;。</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "</ul>\n",
    "\n",
    "<p>请你返回重新排列 <code>nums</code>&nbsp;数组后的结果数组。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [9,12,5,10,14,3,10], pivot = 10\n",
    "<b>输出：</b>[9,5,3,10,10,12,14]\n",
    "<b>解释：</b>\n",
    "元素 9 ，5 和 3 小于 pivot ，所以它们在数组的最左边。\n",
    "元素 12 和 14 大于 pivot ，所以它们在数组的最右边。\n",
    "小于 pivot 的元素的相对位置和大于 pivot 的元素的相对位置分别为 [9, 5, 3] 和 [12, 14] ，它们在结果数组中的相对顺序需要保留。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [-3,4,3,2], pivot = 2\n",
    "<b>输出：</b>[-3,2,4,3]\n",
    "<b>解释：</b>\n",
    "元素 -3 小于 pivot ，所以在数组的最左边。\n",
    "元素 4 和 3 大于 pivot ，所以它们在数组的最右边。\n",
    "小于 pivot 的元素的相对位置和大于 pivot 的元素的相对位置分别为 [-3] 和 [4, 3] ，它们在结果数组中的相对顺序需要保留。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>\n",
    "\t<li><code>pivot</code>&nbsp;等于&nbsp;<code>nums</code>&nbsp;中的一个元素。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [partition-array-according-to-given-pivot](https://leetcode.cn/problems/partition-array-according-to-given-pivot/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [partition-array-according-to-given-pivot](https://leetcode.cn/problems/partition-array-according-to-given-pivot/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[9,12,5,10,14,3,10]\\n10', '[-3,4,3,2]\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        def key_f(n):\n",
    "            if n < pivot:\n",
    "                return -1\n",
    "            if n == pivot:\n",
    "                return 0\n",
    "            return 1\n",
    "        nums.sort(key = key_f)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        def key_f(n):\n",
    "            if n < pivot:\n",
    "                return -1\n",
    "            if n == pivot:\n",
    "                return 0\n",
    "            return 1\n",
    "        nums.sort(key = key_f)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        def key_f(n):\n",
    "            return -1 if n < pivot else 0 if n == pivot else 1\n",
    "        nums.sort(key = key_f)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        def key_f(n):\n",
    "            if n < pivot:\n",
    "                return -1\n",
    "            if n == pivot:\n",
    "                return 0\n",
    "            return 1\n",
    "        nums.sort(key = key_f)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, s: List[int], p: int) -> List[int]:\n",
    "        t = [[] for _ in range(3)]\n",
    "        for i in s:\n",
    "            c = 1\n",
    "            if i < p:\n",
    "                c = 0\n",
    "            if i > p:\n",
    "                c = 2\n",
    "            t[c].append(i)\n",
    "        return t[0] + t[1] + t[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        L=len(nums)\n",
    "        ans=[]\n",
    "        small=0\n",
    "        big=0\n",
    "        equal=0\n",
    "        for i in range(L):\n",
    "            if nums[i]<pivot:\n",
    "                ans.insert(small,nums[i])\n",
    "                small+=1\n",
    "                big+=1\n",
    "                equal+=1\n",
    "            elif nums[i]==pivot:\n",
    "                ans.insert(equal,nums[i])\n",
    "                big+=1\n",
    "                equal+=1\n",
    "            else:\n",
    "                ans.insert(big,nums[i])\n",
    "                big+=1\n",
    "        return ans\n",
    "\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        small,equal,big = [],[],[]\n",
    "        for num in nums:\n",
    "            if num < pivot:\n",
    "                small.append(num)\n",
    "            elif num == pivot:\n",
    "                equal.append(num)\n",
    "            else:\n",
    "                big.append(num)\n",
    "        return small + equal + big"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l = []\n",
    "        m = []\n",
    "        r = []\n",
    "        for i in nums:\n",
    "            if i < pivot:\n",
    "                l.append(i)\n",
    "            elif i > pivot:\n",
    "                r.append(i)\n",
    "            else:\n",
    "                m.append(i)\n",
    "        return l + m + r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        return [x for x in nums if x < pivot] + [x for x in nums if x == pivot] + [x for x in nums if x > pivot]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        less,eq,more = [],[],[]\n",
    "        for i in nums:\n",
    "            if i < pivot:\n",
    "                less.append(i)\n",
    "            elif i == pivot:\n",
    "                eq.append(i)\n",
    "            else:\n",
    "                more.append(i)\n",
    "        return less+eq+more"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        lt, eq, gt = [], [], []\n",
    "        for x in nums:\n",
    "            if x < pivot:\n",
    "                lt.append(x)\n",
    "            elif x == pivot:\n",
    "                eq.append(x)\n",
    "            else:\n",
    "                gt.append(x)\n",
    "        return lt + eq + gt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l,m,r = [],[],[]\n",
    "        for i in nums:\n",
    "            if i < pivot:\n",
    "                l.append(i)\n",
    "            elif i > pivot:\n",
    "                r.append(i)\n",
    "            else:\n",
    "                m.append(i)\n",
    "        return l+m+r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        #快排一趟\n",
    "\n",
    "        res = []\n",
    "\n",
    "        for num in nums:\n",
    "            if num < pivot:\n",
    "                res.append(num)\n",
    "\n",
    "        for num in nums:\n",
    "            if num == pivot:\n",
    "                res.append(num)\n",
    "\n",
    "        for num in nums:\n",
    "            if num > pivot:\n",
    "                res.append(num)\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        a,b,c = [],[],[]\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                a.append(i)\n",
    "            elif i==pivot:\n",
    "                b.append(i)\n",
    "            else:\n",
    "                c.append(i)\n",
    "        return a+b+c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        ans = [pivot] * len(nums)\n",
    "        l, r = 0, len(nums)-1\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] < pivot:\n",
    "                ans[l] = nums[i]\n",
    "                l += 1\n",
    "            if nums[len(nums)-1-i] > pivot:\n",
    "                ans[r] = nums[len(nums)-1-i]\n",
    "                r -= 1\n",
    "        return ans         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        left,mid,right = [],[],[]\n",
    "        for j in nums:\n",
    "            if j < pivot:\n",
    "                left.append(j)\n",
    "            elif j == pivot:\n",
    "                mid.append(j)\n",
    "            else:\n",
    "                right.append(j)\n",
    "        return left+mid+right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l = [[] for i in range(3)]\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] < pivot:\n",
    "                l[0].append(nums[i])\n",
    "            elif nums[i] == pivot:\n",
    "                l[1].append(nums[i])\n",
    "            else:\n",
    "                l[2].append(nums[i])\n",
    "        res = []\n",
    "        for i in range(3):\n",
    "            for j in range(len(l[i])):\n",
    "                value = l[i][j]\n",
    "                res.append(value)\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        a, b, c = [], [], []\n",
    "        for num in nums:\n",
    "            if num == pivot:\n",
    "                a.append(num)\n",
    "            elif num > pivot:\n",
    "                b.append(num)\n",
    "            else:\n",
    "                c.append(num)\n",
    "        ans = c + a + b\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        left = []\n",
    "        medium = []\n",
    "        right = []\n",
    "        for n in nums:\n",
    "            if n < pivot:\n",
    "                left.append(n)\n",
    "            elif n == pivot:\n",
    "                medium.append(n)\n",
    "            else:\n",
    "                right.append(n)\n",
    "        left.extend(medium)\n",
    "        left.extend(right)\n",
    "        return left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from string import *\n",
    "from re import *\n",
    "from datetime import *\n",
    "from collections import *\n",
    "from heapq import *\n",
    "from bisect import *\n",
    "from copy import *\n",
    "from math import *\n",
    "from random import *\n",
    "from statistics import *\n",
    "from itertools import *\n",
    "from functools import *\n",
    "from operator import *\n",
    "from io import *\n",
    "from sys import *\n",
    "from json import *\n",
    "from builtins import *\n",
    "from typing import *\n",
    "\n",
    "\"\"\"\n",
    "You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the\n",
    "following conditions are satisfied:\n",
    "\n",
    " * Every element less than pivot appears before every element greater than pivot.\n",
    " * Every element equal to pivot appears in between the elements less than and greater than pivot.\n",
    " * The relative order of the elements less than pivot and the elements greater than pivot is\n",
    "   maintained.\n",
    "   * More formally, consider every pi, pj where pi is the new position of the ith element and pj is\n",
    "     the new position of the jth element. For elements less than pivot, if i < j and nums[i] < pivot\n",
    "     and nums[j] < pivot, then pi < pj. Similarly for elements greater than pivot, if i < j and\n",
    "     nums[i] > pivot and nums[j] > pivot, then pi < pj.\n",
    "\n",
    "Return nums after the rearrangement.\n",
    "\n",
    " \n",
    "\n",
    "Example 1:\n",
    "\n",
    "\n",
    "Input: nums = [9,12,5,10,14,3,10], pivot = 10\n",
    "Output: [9,5,3,10,10,12,14]\n",
    "Explanation: \n",
    "The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.\n",
    "The elements 12 and 14 are greater than the pivot so they are on the right side of the array.\n",
    "The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings.\n",
    "\n",
    "\n",
    "Example 2:\n",
    "\n",
    "\n",
    "Input: nums = [-3,4,3,2], pivot = 2\n",
    "Output: [-3,2,4,3]\n",
    "Explanation: \n",
    "The element -3 is less than the pivot so it is on the left side of the array.\n",
    "The elements 4 and 3 are greater than the pivot so they are on the right side of the array.\n",
    "The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "Constraints:\n",
    "\n",
    " * 1 <= nums.length <= 105\n",
    " * -106 <= nums[i] <= 106\n",
    " * pivot equals to an element of nums.\n",
    "\"\"\"\n",
    "\n",
    "MOD = 1000000007 # 998244353\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        A, B, C = [], [], []\n",
    "        for x in nums:\n",
    "            if x < pivot:\n",
    "                A.append(x)\n",
    "            elif x == pivot:\n",
    "                B.append(x)\n",
    "            else:\n",
    "                C.append(x)\n",
    "        return A + B + C\n",
    "\n",
    "# testcases = [\n",
    "#     [],\n",
    "#     # [],\n",
    "# ]\n",
    "\n",
    "# s = Solution()\n",
    "# func_name = dir(s)[-1]\n",
    "# func = getattr(s, func_name)\n",
    "\n",
    "# for args in testcases:\n",
    "#     print(func(*args))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def pivotArray(self, nums, pivot):\n",
    "        l1, l2, l3 = [], [], []\n",
    "        for n in nums:\n",
    "            if   n <  pivot: l1.append(n)\n",
    "            elif n == pivot: l2.append(n)\n",
    "            else           : l3.append(n)\n",
    "        return l1 + l2 + l3        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        A,B,C=[],[],[]\n",
    "        for num in nums:\n",
    "            if num<pivot:\n",
    "                A.append(num)\n",
    "            elif num==pivot:\n",
    "                B.append(num)\n",
    "            else:\n",
    "                C.append(num)\n",
    "        return A+B+C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        low = []\n",
    "        equal = []\n",
    "        high = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] < pivot:\n",
    "                low.append(nums[i])\n",
    "            elif nums[i] == pivot:\n",
    "                equal.append(nums[i])\n",
    "            else:\n",
    "                high.append(nums[i])\n",
    "        return low+equal+high"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        small = []\n",
    "        alist = []\n",
    "        large = []\n",
    "        for num in nums:\n",
    "            if num < pivot:\n",
    "                small.append(num)\n",
    "            elif num == pivot:\n",
    "                alist.append(num)\n",
    "            elif num > pivot:\n",
    "                large.append(num)\n",
    "        return small+alist+large"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        a = []\n",
    "        b = []\n",
    "        c = []\n",
    "        for num in nums:\n",
    "            if num < pivot:\n",
    "                a.append(num)\n",
    "            elif num == pivot:\n",
    "                b.append(num)\n",
    "            else:\n",
    "                c.append(num)\n",
    "        return a + b + c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l,mid,r=[],[],[]\n",
    "        for n in nums:\n",
    "            if n<pivot:\n",
    "                l.append(n)\n",
    "            elif n==pivot:\n",
    "                mid.append(n)\n",
    "            else:\n",
    "                r.append(n)\n",
    "        return l+mid+r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]: \n",
    "        if not nums: return nums\n",
    "        \n",
    "        length = len(nums)\n",
    "        res = [pivot] * length\n",
    "        left, right = 0, length - 1\n",
    "        for idx, value in enumerate(nums):\n",
    "            if value < pivot:\n",
    "                res[left] = value\n",
    "                left += 1\n",
    "            elif value > pivot:\n",
    "                res[right] = value\n",
    "                right -= 1\n",
    "        res[right + 1:] = res[right + 1:][::-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        return [x for x in nums if x < pivot]+[x for x in nums if x==pivot]+[x for x in nums if x >pivot]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        less=list()\n",
    "        more=list()\n",
    "        equal=list()\n",
    "        for num in nums:\n",
    "            if pivot>num: less.append(num)\n",
    "            elif pivot==num: equal.append(num)\n",
    "            else: more.append(num)\n",
    "        return less+equal+more"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]: \n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1 \n",
    "        if lf01:\n",
    "            l06 = l5[la1+1:]\n",
    "            l06.reverse()\n",
    "            return l5[:la1+1]  + l06\n",
    "        else:\n",
    "            return l5[:la1+1]  + l5[la1+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1\n",
    "        l6 = l5[la1+1:]\n",
    "        if lf01:\n",
    "            l6.reverse()\n",
    "        l5 = l5[:la1+1]  + l6\n",
    "        return l5 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1\n",
    "        l6 = l5[la1+1:]\n",
    "        if lf01:  l6.reverse() \n",
    "        return  l5[:la1+1]  + l6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1\n",
    "        l6 = l5[la1+1:]\n",
    "        if lf01:  l6.reverse() \n",
    "        return  l5[:la1+1]  + l6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1\n",
    "        l6 = l5[la1+1:]\n",
    "        if lf01:\n",
    "            l6.reverse()\n",
    "        l5 = l5[:la1+1]  + l6\n",
    "        return l5 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1\n",
    "        l6 = l5[la1+1:]\n",
    "        if lf01:\n",
    "            l6.reverse() \n",
    "        return  l5[:la1+1]  + l6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        res = []\n",
    "        stack1 = []\n",
    "        stack2 = []\n",
    "        count = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                stack1.append(i)\n",
    "            elif i==pivot:\n",
    "                count+=1\n",
    "            else:stack2.append(i)\n",
    "        res = stack1+[pivot]*count+stack2\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        a1, a2, a3 = [], [], []\n",
    "        for i in nums:\n",
    "            if i < pivot:\n",
    "                a1.append(i)\n",
    "            elif i == pivot:\n",
    "                a2.append(i)\n",
    "            else:\n",
    "                a3.append(i)\n",
    "        return a1+a2+a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        res = [pivot]*sum([1 for i in nums if i==pivot])\n",
    "        res2 = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] < pivot:\n",
    "                res2.append(nums[i])\n",
    "            elif nums[i] > pivot:\n",
    "                res.append(nums[i])\n",
    "        return res2+res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        less,more,equal=[],[],[]\n",
    "        for i in nums:\n",
    "            if i<pivot:less.append(i)\n",
    "            elif i>pivot:more.append(i)\n",
    "            else:equal.append(i)\n",
    "        return less+equal+more"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        i = 0\n",
    "        big = []\n",
    "        small = []\n",
    "        medium = []\n",
    "        for i in nums:\n",
    "            if i < pivot:\n",
    "                small.append(i)\n",
    "            if i == pivot:\n",
    "                medium.append(i)\n",
    "            if i > pivot:\n",
    "                big.append(i)\n",
    "        return small + medium + big "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        length = len(nums)\n",
    "        res = [pivot] * length\n",
    "        left, right = 0, length - 1\n",
    "        for idx, value in enumerate(nums):\n",
    "            if value < pivot:\n",
    "                res[left] = value\n",
    "                left += 1\n",
    "            elif value > pivot:\n",
    "                res[right] = value\n",
    "                right -= 1\n",
    "        if right < length - 1:\n",
    "            res[-(length - right - 1):] = res[-(length - right - 1):][::-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        ans = []\n",
    "        stack = []\n",
    "        cnt = 0\n",
    "        for num in nums:\n",
    "            if pivot == num:\n",
    "                cnt += 1\n",
    "            elif pivot < num:\n",
    "                stack.append(num)\n",
    "            else:\n",
    "                ans.append(num)\n",
    "        return ans + [pivot] * cnt + stack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        minums, manums, pinums = [], [], []\n",
    "        for val in nums:\n",
    "            if val < pivot:\n",
    "                minums.append(val)\n",
    "            elif val == pivot:\n",
    "                pinums.append(val)\n",
    "            else:\n",
    "                manums.append(val)\n",
    "            \n",
    "        return minums + pinums + manums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        res = []\n",
    "        for num in nums:\n",
    "            if num < pivot:\n",
    "                res.append(num)\n",
    "        for num in nums:\n",
    "            if num == pivot:\n",
    "                res.append(num)\n",
    "        for num in nums:\n",
    "            if num>pivot:\n",
    "                res.append(num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        a = []\n",
    "        b = []\n",
    "        c = []\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                a.append(i)\n",
    "            elif i==pivot:\n",
    "                b.append(i)\n",
    "            elif i>pivot:\n",
    "                c.append(i)  \n",
    "        return a+b+c     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        lst1 = []\n",
    "        lst2 = []\n",
    "        lst3 = []\n",
    "        for i in nums:\n",
    "            if i < pivot:\n",
    "                lst1.append(i)\n",
    "            elif i == pivot:\n",
    "                lst2.append(i)\n",
    "            else:\n",
    "                lst3.append(i)\n",
    "        return lst1 + lst2 + lst3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        res = []\n",
    "        for num in nums:\n",
    "            if num < pivot:\n",
    "                res.append(num)\n",
    "        for num in nums:\n",
    "            if num == pivot:\n",
    "                res.append(num)\n",
    "        for num in nums:\n",
    "            if num>pivot:\n",
    "                res.append(num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        n=len(nums)\n",
    "        res=[0]*n\n",
    "        p=0\n",
    "        for i in range(n):\n",
    "            if nums[i]<pivot:\n",
    "                res[p]=nums[i]\n",
    "                p+=1\n",
    "        for i in range(n):\n",
    "            if nums[i]==pivot:\n",
    "                res[p]=nums[i]\n",
    "                p+=1\n",
    "        for i in range(n):\n",
    "            if nums[i]>pivot:\n",
    "                res[p]=nums[i]\n",
    "                p+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        a,b,c = [],[],[]\n",
    "        for x in nums:\n",
    "            if x < pivot: a.append(x)\n",
    "            elif x == pivot: b.append(x)\n",
    "            else: c.append(x)\n",
    "        return a + b + c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        a,b,c=[],[],[]\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                a.append(i)\n",
    "            elif i==pivot:\n",
    "                b.append(i)\n",
    "            else:\n",
    "                c.append(i)\n",
    "        return a+b+c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        return sorted(nums, key = lambda x: (x > pivot, x == pivot))\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        ret = []\n",
    "        for num in nums:\n",
    "            if num < pivot:\n",
    "                ret.append(num)\n",
    "        for num in nums:\n",
    "            if num == pivot:\n",
    "                ret.append(num)\n",
    "        for num in nums:\n",
    "            if num > pivot:\n",
    "                ret.append(num)\n",
    "        return ret\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        A, B, C = [], [], []\n",
    "        for num in nums:\n",
    "            if num < pivot:\n",
    "                A.append(num)\n",
    "            elif num == pivot:\n",
    "                B.append(num)\n",
    "            else:\n",
    "                C.append(num)\n",
    "        return A + B + C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        left = [x for x in nums if x < pivot]\n",
    "        right = [x for x in nums if x > pivot]\n",
    "        same = [pivot] * nums.count(pivot)\n",
    "        return left + same + right\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1 \n",
    "        if lf01:\n",
    "            l06 = l5[la1+1:]\n",
    "            l06.reverse()\n",
    "            return l5[:la1+1]  + l06\n",
    "        else:\n",
    "            return l5[:la1+1]  + l5[la1+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1 \n",
    "        if lf01:\n",
    "            l06 = l5[la1+1:]\n",
    "            l06.reverse()\n",
    "            return l5[:la1+1]  + l06\n",
    "        else:\n",
    "            return l5[:la1+1]  + l5[la1+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        nums.sort(key = lambda x:(x>pivot, x==pivot))\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        nums.sort(key = lambda x:(x>pivot, x==pivot))\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1\n",
    "        l6 = l5[la1+1:]\n",
    "        if lf01:\n",
    "            l6.reverse() \n",
    "        return  l5[:la1+1]  + l6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1\n",
    "        l6 = l5[la1+1:]\n",
    "        if lf01:\n",
    "            l6.reverse()\n",
    "        l5 = l5[:la1+1]  + l6\n",
    "        return l5 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]: \n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1 \n",
    "        if lf01:\n",
    "            l06 = l5[la1+1:]\n",
    "            l06.reverse()\n",
    "            return l5[:la1+1]  + l06\n",
    "        else:\n",
    "            return l5[:la1+1]  + l5[la1+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1 \n",
    "        if lf01:\n",
    "            l06 = l5[la1+1:]\n",
    "            l06.reverse()\n",
    "            return l5[:la1+1]  + l06\n",
    "        else:\n",
    "            return l5[:la1+1]  + l5[la1+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1,la1,mCnt = 0,-1,0\n",
    "        lf01 = 0\n",
    "        for i in nums:\n",
    "            if i<pivot:\n",
    "                l5[p1] = i\n",
    "                p1+=1 \n",
    "            elif i>pivot:\n",
    "                l5[la1] = i\n",
    "                la1-=1\n",
    "                lf01 = 1 \n",
    "        if lf01:\n",
    "            l06 = l5[la1+1:]\n",
    "            l06.reverse()\n",
    "            return l5[:la1+1]  + l06\n",
    "        else:\n",
    "            return l5[:la1+1]  + l5[la1+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        ans = []\n",
    "        ans2 = []\n",
    "        count = 0\n",
    "        for i in range(n):\n",
    "            if nums[i]<pivot:\n",
    "                ans.append(nums[i])\n",
    "            elif nums[i]>pivot:\n",
    "                ans2.append(nums[i])\n",
    "            else:\n",
    "                count += 1\n",
    "        for i in range(count):\n",
    "            ans.append(pivot)\n",
    "        # for i in range(len(ans2)):\n",
    "        #     ans.append(ans2[i])\n",
    "        return ans+ans2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1, la1, mCnt = 0, -1, 0 \n",
    "        for i in nums:\n",
    "            if i < pivot:\n",
    "                l5[p1] = i\n",
    "                p1 += 1\n",
    "            elif i > pivot:\n",
    "                l5[la1] = i\n",
    "                la1 -= 1 \n",
    "        la2 = -1\n",
    "        la1 += 1\n",
    "        while la2 > la1:\n",
    "            l5[la2], l5[la1] = l5[la1], l5[la2]\n",
    "            la2 -= 1\n",
    "            la1 += 1\n",
    "        return l5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        l = 0\n",
    "        r = n - 1\n",
    "        ans = [pivot] * n\n",
    "        for i in range(n):\n",
    "            if nums[i] < pivot:\n",
    "                ans[l] = nums[i]\n",
    "                l += 1\n",
    "            if nums[n - i - 1] > pivot:\n",
    "                ans[r] = nums[n - i - 1]\n",
    "                r -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        arr = []\n",
    "        t = 0\n",
    "        for i in nums:\n",
    "            if i < pivot:\n",
    "                arr.append(i)\n",
    "            elif i == pivot:\n",
    "                t += 1\n",
    "        for _ in range(t):\n",
    "            arr.append(pivot)\n",
    "        for i in nums:\n",
    "            if i > pivot:\n",
    "                arr.append(i)\n",
    "        return arr\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        length = len(nums)\n",
    "        res = [pivot] * length\n",
    "        left, right = 0, length - 1\n",
    "        for idx, value in enumerate(nums):\n",
    "            if value < pivot:\n",
    "                res[left] = value\n",
    "                left += 1\n",
    "            elif value > pivot:\n",
    "                res[right] = value\n",
    "                right -= 1\n",
    "        if right < length - 1:\n",
    "            res[-(length - right - 1):] = res[-(length - right - 1):][::-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in nums]\n",
    "        p1, la1, mCnt = 0, -1, 0 \n",
    "        for i in nums:\n",
    "            if i < pivot:\n",
    "                l5[p1] = i\n",
    "                p1 += 1\n",
    "            elif i > pivot:\n",
    "                l5[la1] = i\n",
    "                la1 -= 1 \n",
    "        la2 = -1\n",
    "        la1 += 1\n",
    "        while la2 > la1:\n",
    "            l5[la2], l5[la1] = l5[la1], l5[la2]\n",
    "            la2 -= 1\n",
    "            la1 += 1\n",
    "        return l5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        ret = []\n",
    "        c = 0\n",
    "        for num in nums:\n",
    "            if num > pivot:\n",
    "                c += 1\n",
    "            ret.append(pivot)\n",
    "        i1 = 0\n",
    "        i2 = len(nums) - c\n",
    "        for num in nums:\n",
    "            if num > pivot:\n",
    "                ret[i2] = num\n",
    "                i2 += 1\n",
    "            elif num < pivot:\n",
    "                ret[i1] = num\n",
    "                i1 += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n",
    "        l4 = len(nums)\n",
    "        l5 = [pivot for i in range(l4)]\n",
    "        p1, la1, mCnt = 0, -1, 0 \n",
    "        for i in nums:\n",
    "            if i < pivot:\n",
    "                l5[p1] = i\n",
    "                p1 += 1\n",
    "            elif i > pivot:\n",
    "                l5[la1] = i\n",
    "                la1 -= 1 \n",
    "        la2 = -1\n",
    "        la1 += 1\n",
    "        while la2 > la1:\n",
    "            l5[la2], l5[la1] = l5[la1], l5[la2]\n",
    "            la2 -= 1\n",
    "            la1 += 1\n",
    "        return l5"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
